package com.lis.scf.security;


import com.alibaba.fastjson.JSON;
import com.lis.scf.common.dto.ResponseDTO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 登录失败处理器
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException {
        // 构建失败响应
        response.setContentType("application/json;charset=UTF-8");
        ResponseDTO<Object> fail = ResponseDTO.fail(401, "登录失败: " + exception.getMessage());
        response.getWriter().write(JSON.toJSONString(fail));
    }
}
